<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\OrderSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '订单';
$this->params['breadcrumbs'][] = $this->title;
$infoMap=[];
?>
<div class="order-index">
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            'order_id',
            'uid',
            [
                'attribute' => 'ctime',
                'content' => function($model, $key, $index, $column) {
                    return date('Y/m/d H:i', $model->ctime/1000);
                },
                'filter' => false,
            ],
            [
                'class' => 'yii\grid\Column',
                'header' => '出行日期',
                'content' => function($model) use (&$infoMap) {
                    if (!isset($infoMap[$model->id])) {
                        $trip = \common\models\Trip::getOne("id=" . $model->trip_id);
                        $boat = \common\models\Boat::getOne("id={$trip['id']}");
                        $infoMap[$model->id] = [
                            'trip' => $trip,
                            'boat' => $boat
                        ];
                    }
                    $startTime = $infoMap[$model->id]['trip']['start_time'];
                    return date('Y-m-d', strtotime($startTime));
                }
            ],
            [
                'class' => 'yii\grid\Column',
                'header' => '国家',
                'content' => function($model) use (&$infoMap) {
                    return $infoMap[$model->id]['boat']['countries_cn'];
                }
            ],
            [
                'class' => 'yii\grid\Column',
                'header' => '船名',
                'content' => function($model) use (&$infoMap) {
    			    return $infoMap[$model->id]['boat']['name_cn'] ?: $infoMap[$model->id]['boat']['name_en'];
                }
            ],
//            [
//                'attribute' => 'utime',
//                'content' => function($model, $key, $index, $column) {
//                    return date('Y/m/d H:i', $model->utime/1000);
//                },
//                'filter' => false,
//            ],
            [
                'class' => 'yii\grid\Column',
				'header'=>'支付方式',
                'content' => function($model) {
                    $payment = $model->payment ? json_decode($model->payment, true) : ['type'=>''];
                    if ($payment['type'] == \common\logic\Order::PAY_TYPE_ALI) {
                        return '支付宝';
                    }
                }
            ],
            [
             'attribute' => 'price',
             'content' => function($model) {
                 return '￥' . $model->price / 100;
             }
            ],
			//'unitprice',
            [
                'attribute' => 'order_status',
                'content' => function($model, $key, $index, $column) {
                    return \common\logic\OrderStatus::TEXT[$model->order_status];
                },
                'filter' => \common\logic\OrderStatus::TEXT,
            ],
            [
                'attribute' => 'supply_status',
                'content' => function($model, $key, $index, $column) {
                    return \common\logic\SupplyStatus::TEXT[$model->supply_status];
                },
                'filter' => \common\logic\SupplyStatus::TEXT,
            ],
            [
                'attribute' => 'payment_status',
                'content' => function($model, $key, $index, $column) {
                    return \common\logic\PaymentStatus::TEXT[$model->payment_status];
                },
                'filter' => \common\logic\PaymentStatus::TEXT,
            ],
            [
                'attribute' => 'account_status',
                'content' => function($model, $key, $index, $column) {
                    return \common\logic\AccountStatus::TEXT[$model->account_status];
                },
                'filter' => \common\logic\AccountStatus::TEXT,
            ],
            [
                'attribute' => 'deleted',
                'content' => function($model, $key, $index, $column) {
                    return $model->deleted ? '<span class="text-danger">是</span>':'<span class="text-success">否</span>';
                },
                'filter' => [
                    \common\models\Order::DELETED => '是',
                    \common\models\Order::NOT_DELETED => '否'
                ],
            ],
            'trip_id',
           // 'room_ids',
            //'contact',
            //'ext:ntext',
            //'payment:ntext',
            'email:email',
            'phone',
            //'fund_amount',
            //'use_protect',
            //'message',
			'cabin_id',
            [
                'attribute' => 'one_self',
                'content' => function($model, $key, $index, $column) {
                    return $model->one_self ? '独占':'共享';
                },
                'filter' => [
                    \common\models\Order::ONE_SELF => '独占',
                    \common\models\Order::NOT_ONE_SELF => '共享'
                ],
            ],
            [
                'attribute' => 'occupancy_id',
                'content' => function($model, $key, $index, $column) {
                    return $model->occupancy_id;
                },
                'filter' => false,
            ],


            ['class' => 'yii\grid\ActionColumn','header' => '操作',
             'contentOptions' => ['class' => 'action-btn-td'],
             'buttons' => [
                 'update' => function($url, $model, $key) {
                     $options = [
                         'class' => 'btn btn-sm btn-default modify-btn',
                         'aria-label' => '修改',
                     ];
                     return Html::a('<i class="fa fa-pencil-square-o"></i>', $url, $options);
                 },
                 'delete' => function($url, $model, $key) {
                     $options = [
                         'class' => 'btn btn-sm btn-default',
                         'aria-label' => Yii::t('yii', 'Delete'),
                         'data-confirm' => '确定删除此订单',
                         'data-method' => 'post',
                         'data-pjax' => '0',
                     ];
                     return Html::a('<span class="fa fa-trash"></span>', $url, $options);
                 },
             ],
             'template' => '{update} {hide} {delete}',],
        ],
    ]); ?>
</div>
